package com.sam.im.heiqun.uis.activities;

import android.content.Context;
import android.content.ContextWrapper;
import android.content.Intent;
import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.VideoView;

import com.sam.im.heiqun.R;
import com.sam.im.heiqun.constant.Constant;
import com.yuyh.library.utils.data.PrefsUtils;

public class HelloMobileYXActivity extends AppCompatActivity implements  View.OnClickListener, MediaPlayer.OnCompletionListener {

    private static final String TAG = HelloMobileYXActivity.class.getSimpleName();

    VideoView mVideoView;

    TextView jump;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_hello_mobile_yx);
        initView();
    }

    private void initView() {

        mVideoView = (VideoView) findViewById(R.id.hello_video_view);
        jump = (TextView) findViewById(R.id.jump);

        String path = "android.resource://" + getPackageName() + "/"+ R.raw.mobileyxhello;
        mVideoView.setVideoPath(path);
        mVideoView.setOnCompletionListener(this);
        mVideoView.start();

        jump.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.jump:
                doJump();
                break;
        }
    }

    private void doJump() {
        new PrefsUtils(this, Constant.IS_FIRST).put(Constant.IS_FIRST,"1");
        Intent intent = new Intent(this, LoginActivity.class);
        startActivity(intent);
        finish();
    }

    @Override
    public void onCompletion(MediaPlayer mp) {
        doJump();
    }

    //重写此方法避免传入context造成内存泄漏
    @Override
    protected void attachBaseContext(Context newBase)
    {
        super.attachBaseContext(new ContextWrapper(newBase)
        {
            @Override
            public Object getSystemService(String name)
            {
                if (Context.AUDIO_SERVICE.equals(name))
                    return getApplicationContext().getSystemService(name);
                return super.getSystemService(name);
            }
        });
    }
}
